package com.cn.ycy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.net.URISyntaxException;

@SpringBootApplication()
public class YcyGatewaySpringRun {

  public static void main(String[] args) {
    SpringApplication.run(YcyGatewaySpringRun.class, args);
  }

  @Bean
  public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    RouteLocatorBuilder.Builder routes = builder.routes();
    //basic proxy
    routes.route(r -> r.path("/baidu")
            .uri("http://baidu.com:80/")
    );


    return routes.build();
  }

  public static void test() {
    try {
      URI uri = new URI("http://www.baidu.com");
      String query = "helloword=111";
      URI newUri = UriComponentsBuilder.fromUri(uri)
              .replaceQuery(query.toString()).build(true).toUri();
      System.out.println(newUri);
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
  }


}
